package com.qf.web.servlet;

import com.qf.domain.Goods;
import com.qf.domain.PageBen;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;

//商品分类
@WebServlet(value = "/goodsservlet")
public class GoodsServlet extends BaseServlet {
    //分页查询
    public String getGoodsListByTypeId(HttpServletRequest request, HttpServletResponse response){
        try {
            //获取pagename
            String pagename = request.getParameter("pageNum");
            //获取pagesize
            String pagesize = request.getParameter("pageSize");
            //设置初始页数为   1    大小为8

            int page_name=1;
            int page_size=8;
            //如果pagename不为空

            if (!StringUtils.empty(pagename)){
                //字符串转换为Integer给page——nam

                page_name=Integer.parseInt(pagename);
                //如果page_num小于1页那么等于1

                if (page_name<1){
                    page_name=1;
                }
            }
            //判断pagesize是否为空

            if (!StringUtils.empty(pagesize)){
                //字符串转换为Integer给page——nam
                page_size=Integer.parseInt(pagesize);
                //如果page_size大小那么等于8

                if (page_size<1){
                    page_size=8;
                }
            }
            //获取typeid

            //typeid指当前页数
            String typeid = request.getParameter("typeId");
            String name = request.getParameter("name");
            ArrayList<Object> list=new ArrayList<>();
            //定义一个where字符串

            String where=" where 1=1";
            //判断typeid是否为空

            if (!StringUtils.empty(typeid)){
                where+=" and typeid=?";
                list.add(typeid);
            }
            if (!StringUtils.empty(name)){
                where+=" and name like ?";
                list.add(name);
            }
            //调用业务
            GoodsService goodsService=new GoodsServiceImpl();
            //查询业务有关的类
            PageBen<Goods> pageBen=goodsService.findPage(page_name,page_size,where,list);
            pageBen.toString();

//            for (Goods goods: pageBen.getData()){
//                System.out.println(goods);
//            }

            request.setAttribute("pageBean",pageBen);
            request.setAttribute("typeId",typeid);
            return "/goodsList.jsp";
        }catch (Exception e){
            e.printStackTrace();
            request.setAttribute("msg","分页有问题");
            return "/message.jsp";
        }
    }

    //商品详情
    public String getGoodsById(HttpServletRequest request, HttpServletResponse response){
        //获取 id
         System.out.println("到这了");
        String id = request.getParameter("id");
        if (StringUtils.empty(id)){
            //判断是否为空，为空跳转
            request.setAttribute("msg","id为空");
            System.out.println("到这了");
            return "/message.jsp";
        }else {
            //调用findByid（goodid）业务 返回商品
            GoodsService goodsService=new GoodsServiceImpl();
            //加方法
            Goods goods = goodsService.findByid(Integer.parseInt(id));
            request.setAttribute("goods",goods);
            System.out.println("到这了");
            return "/goodsDetail.jsp";
        }


    }


}
